<?php
/**
 * User: 徐剑锋(xuic)
 * Date: 2016/10/18
 * Time: 12:36
 */


namespace work;

use app\ctr;
use C\lib\Db;

use C\mod\finance;

class finances extends base{

    private static $paginate = 10;

    // --- 财务 ----
    public function financeList() {

        if(($post = $this->getAesPost()) && ($worker = $this->getLogWorker())) {

            $stmt = Db::query('SELECT type FROM ' . DB_PRE . 'worker_type WHERE worker_id ="' . $worker->id . '" and type=1');

            if( $stmt && $stmt->rowCount() > 0) {

                date_default_timezone_set('PRC');
                $sqlLimit = isset($post['lastId']) && $post['lastId']!=0 ? 'and id < '.$post['lastId'] : 'and id >= 0 ';


                $f = [];
                $financeList = [];
                $finance = finance::get(['worker_id'=> $worker->id]);
                if($finance) {
                   $f['id'] = $finance->id;
                   $f['balance'] = $finance->balance;
                   $f['time_update'] = date('Y-m-d H:i:s', $finance->time_update);

                   $t = explode(' ', $_SERVER['REQUEST_TIME']);
                   $mixTime = $t[0] - 15 * 24 * 3600; // 15天
                   $stmt = Db::query("select id,FROM_UNIXTIME(time_add,'%Y-%m-%d') days,sum(price) totalPrice from cd_finance_details where worker_id={$worker->id} and time_add>={$mixTime} " . $sqlLimit . ' group by days ORDER BY days DESC LIMIT ' . static::$paginate);

                   $financeList = [];
                   if($stmt && $stmt->rowCount() >= 0) {

                       while($data = $stmt->fetch(\PDO::FETCH_ASSOC)) {
                           $financeList[] = $data;
                       }
                       $this->writeAesJson( 1 , ['jfinance'=> $f , 'jfinanceList'=>$financeList] , 1);
                   }else
                       $this->writeAesJson( 0 , '查询失败' ,1 );
                }else
                    $this->writeAesJson( 0 , '获取财务信息失败或无此工作人员财务信息' ,1 );
            }else
                $this->writeAesJson( 0 , '非集结点' ,1 );
        }

    }


    // --- 当日详情 ---
    public function financeDetail() {

        if(($post = $this->getAesPost()) && ($worker = $this->getLogWorker())) {

            $stmt = Db::query('SELECT type FROM ' . DB_PRE . 'worker_type WHERE worker_id ="' . $worker->id . '" and type=1');
            if( $stmt && $stmt->rowCount() > 0) {

                if(isset($post['fdate']) && trim($post['fdate']) != '') {

                    list($y, $m, $d) = explode('-', $post['fdate']);

                    if(checkdate($m ,  $d , $y )) {

                        date_default_timezone_set('PRC');
                        $startTime = strtotime($y.'-'.$m.'-'.$d.' 0:0:0');
                        $endTime = strtotime($y.'-'.$m.'-'.$d.' 23:59:59');

                        $scan_type = in_array($worker->id ,static::$centerPoint) ? 1 : 0;
                        $stmt1 = Db::query("select * from (select count(distinct piece_id) as getNum from cd_piece_state where worker_id={$worker->id} and state_in=3 and (time_add>{$startTime} and time_add<={$endTime})) a,(select if(isNull(sum(piece_count)),0,sum(piece_count)) as sendNum from cd_finance_details where worker_id={$worker->id} and scan_type=2 and (time_add>{$startTime} and time_add<={$endTime})) b,(select if(isNull(sum(price)),0,sum(price)) as center_charge from cd_finance_details where worker_id={$worker->id}  and scan_type={$scan_type} and (time_add>{$startTime} and time_add<={$endTime})) g,(select count(id) as totalNum from cd_piece_state where worker_id in (" . implode(',',static::$centerPoint) . ")  and (time_add>{$startTime} and time_add<={$endTime}) and state_in in (3,9)) c,(select if(isNull(sum(price)),0,sum(price)) allPerPrice from cd_finance_details where  worker_id={$worker->id} and (time_add>{$startTime} and time_add<={$endTime})) e,(select if(isNull(sum(price)),0,sum(price)) transfer_charge from cd_finance_details where  worker_id={$worker->id} and scan_type=3 and (time_add>{$startTime} and time_add<={$endTime})) f");//'3' => '已集散', '9' => '到集散',


                        if(in_array($worker->id ,static::$centerPoint) ) {
                            $scan_type = 1;

                        }else {
                            $scan_type = 0;
                        }

                        if($data = $stmt1->fetch(\PDO::FETCH_ASSOC)) {
                            // 中心扫描费＝收件数量×1.5元
//                            $data['center_charge'] = $worker->id== static::$centerPoint?bcmul($data['getNum'], 1.5,1):bcmul($data['getNum'], -1.5,1);
                            // 派件费=派件数量×1元
                            if(in_array($worker->id,static::$special_points)) {
                                $data['send_charge'] = bcmul($data['sendNum'], static::$unit_price2,1);
                            }else if($worker->id == static::$leiniao35_id){
                                $data['send_charge'] = bcmul($data['sendNum'], static::$leiniao35,1);
                            }else if($worker->id == static::$leiniao7_id){
                                $data['send_charge'] = bcmul($data['sendNum'], static::$leiniao7,1);
                            }else{
                                $data['send_charge'] = bcmul($data['sendNum'], static::$unit_price,1);
                            }

                            $data['flag'] = in_array($worker->id , static::$centerPoint);
                            // 中转费（站点）＝   $data['transfer_charge']
                            $this->writeAesJson( 1 , ['jdatas'=> $data] , 1);
                        }else
                            $this->writeAesJson( 0 , '获取失败' , 1);
                    }else
                        $this->writeAesJson( 0 , '无效日期格式，正确示例：2016-10-21' ,1 );
                }else
                    $this->writeAesJson( 0 , '无查询财务参数传递' ,1 );
            }else
                $this->writeAesJson( 0 , '非集结点' ,1 );
        }
    }
}

